import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../EcyRouter.dart';
import '../../ecycommon/EcyRequestBaseManager.dart';
import '../../ecycommon/utils/EcyLogger.dart';
import '../../model/EcyApiCommonResponse.dart';
import '../group/EcyGroupListPage.dart';

class EcyMyGroupMainPage extends StatefulWidget {
  late _EcyMyGroupMainPageState state;

  EcyMyGroupMainPage();

  @override
  State<StatefulWidget> createState() {
    state = _EcyMyGroupMainPageState();
    return state;
  }
}

class _EcyMyGroupMainPageState extends State<EcyMyGroupMainPage> {
  _EcyMyGroupMainPageState();

  final EcyGroupListPage myGroupListPage = EcyGroupListPage("1", true, key: const Key("myGroupListPage"),);

  int _draftNum = 0;

  Future<void> getDraftNum() async {
    try {
      var response = await EcyRequestbasemanager()
          .post("/app/api/app/valley/circle/draftNum", {});
      if (response != null) {
        var commonResponse =
          EcyApiCommonResponse.fromJson(response, (response) => json);
        if (commonResponse.code == 0 || commonResponse.code == 200) {
          if(response["data"] != null) {
            setState(() {
              _draftNum = response["data"];
            });
          }
        }
      }
    } catch (e) {
      EcyLogger.log("查询当前人草稿数量$e");
    }
  }

  @override
  void initState() {
    super.initState();
    getDraftNum();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0XFFF5F5F5),
      appBar: AppBar(
          backgroundColor: Colors.white,
          leadingWidth: 50.w,
          leading: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              EcyRouter.pop(context, "");
            },
            child: Align(
              alignment: Alignment.centerRight,
              child: Image.asset("assets/images/ic_back.png", width: 30.w,),
            ),
          ),
          titleSpacing: 0,
          centerTitle: true,
          title: Text("我的动态",
              style: TextStyle(
                  fontFamily: "PingFang SC",
                  fontWeight: FontWeight.w700,
                  fontSize: 16.sp,
                  color: const Color(0xFF262626))),
          actions: [
            GestureDetector(
              onTap: () {
                EcyRouter().push(context, EcyRouter.myGroupDraftListPage, {}).then((val) => getDraftNum());
              },
              child: Text("草稿箱($_draftNum)    ",
                  style: TextStyle(
                      fontFamily: "PingFang SC",
                      fontWeight: FontWeight.w700,
                      fontSize: 16.sp,
                      color: const Color(0xFF262626))),
            ),
          ]),
      body: Padding(
        padding: const EdgeInsets.all(8),
        child: myGroupListPage,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void deactivate() {
    super.deactivate();
  }
}
